package uy.com.csi.gestpro.util;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

import org.primefaces.context.RequestContext;

public class Faces {
	public static void messageAlert(String texto) {
		FacesMessage fm = new FacesMessage(FacesMessage.SEVERITY_WARN, texto, null);
		FacesContext.getCurrentInstance().addMessage(null, fm);
	}

	public static void messageError(String texto) {
		FacesMessage fm = new FacesMessage(FacesMessage.SEVERITY_ERROR, texto, null);
		FacesContext.getCurrentInstance().addMessage(null, fm);
	}

	public static void messageConfirmacion(String texto) {
		FacesMessage fm = new FacesMessage(FacesMessage.SEVERITY_INFO, texto, null);
		FacesContext.getCurrentInstance().addMessage(null, fm);
	}

	public static void muestraDialog(String widgetVar) {
		RequestContext.getCurrentInstance().execute(widgetVar + ".show();");
	}
	
	public static void ocultaDialog(String widgetVar) {
		RequestContext.getCurrentInstance().execute(widgetVar + ".hide();");
	}

	public static void actualizaComponente(String id) {
		RequestContext.getCurrentInstance().update(id);
	}

	public static final void registrarVariable(String nombre, Object variable) {
		FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put(nombre, variable);
	}

	public static final Object getVariable(String nombre) {
		return FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get(nombre);
	}

	public static final Object removeVariable(String nombre) {
		return FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove(nombre);
	}

	public static void cerrarSesion() {
		FacesContext.getCurrentInstance().getExternalContext().getSessionMap().clear();
		HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		session.invalidate();
	}

	public static String redirecciona(String url) {
		return url.concat("?faces-redirect=true");
	}
	
	public static String rutaServidor(){
		return FacesContext.getCurrentInstance().getExternalContext().getRealPath("");
	}
}
